package api

import (
	"FeedXmh/app/dao"
	"fmt"
	jwt "github.com/gogf/gf-jwt"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/os/gtime"
	"github.com/gogf/gf/util/gconv"
)

var Group = group{}

type group struct{}

func (a *group) GET(r *ghttp.Request) {
	res := g.Map{
		"code":  0,
		"msg":   "",
		"data":  nil,
		"count": 0,
	}
	params := r.GetQueryMap()

	id := gconv.String(params["id"])
	key := gconv.String(params["key"])

	claims := jwt.ExtractClaims(r)
	userid := claims["userid"].(string)
	url := r.URL.Path

	//db := g.DB(g.Cfg().GetString("custom.db_name"))
	if dao.CheckApiPermission(url, r.Method, userid) {
		where := " 1=1 "
		//model := db.Model("t_group")
		if id != "" {
			//model.Where(" id=? ", id)
			where += fmt.Sprintf("and id='%s' ", id)
		}
		if key != "" {
			//model.Where(" group_name like '%?%'", key)
			where += "and group_name like '%" + key + "%'"
		}

		list := dao.GetPermisGroup(userid, where)
		res["data"] = list
		res["count"] = len(list)
	} else {
		res["code"] = -1
		res["msg"] = "您没有该操作权限"
	}

	r.Response.WriteJson(res)
	r.ExitAll()
}

func (a *group) PUT(r *ghttp.Request) {
	res := g.Map{
		"code":  0,
		"msg":   "",
		"data":  nil,
		"count": 0,
	}
	params := r.GetFormMap()

	claims := jwt.ExtractClaims(r)
	current_user := claims["userid"].(string)
	url := r.URL.Path

	params["update_date"] = gtime.Now().String()
	params["update_user"] = current_user
	id := gconv.String(params["id"])

	if dao.CheckApiPermission(url, r.Method, current_user) {
		//情况对应t_user_group后，重新添加
		db := g.DB(g.Cfg().GetString("custom.db_name"))
		_, err := db.Model("t_group").Data(params).Save()
		if err != nil {
			res["code"] = -1
			res["msg"] = err.Error()
		} else {
			res["code"] = 0
			res["msg"] = "保存成功"
			res["data"] = id
		}

	} else {
		res["code"] = -1
		res["msg"] = "您没有该操作权限"
	}
	r.Response.WriteJson(res)
	r.ExitAll()
}

func (a *group) POST(r *ghttp.Request) {
	res := g.Map{
		"code":  0,
		"msg":   "",
		"data":  nil,
		"count": 0,
	}
	params := r.GetFormMap()

	_, exist := params["id"]
	if exist {
		delete(params, "id")
	}

	claims := jwt.ExtractClaims(r)
	current_user := claims["userid"].(string)
	url := r.URL.Path
	params["create_date"] = gtime.Now().String()
	params["create_user"] = current_user
	//id:= gconv.String(params["id"])

	//db:=g.DB(g.Cfg().GetString("custom.db_name"))
	if dao.CheckApiPermission(url, r.Method, current_user) {
		db := g.DB(g.Cfg().GetString("custom.db_name"))
		re, err := db.Model("t_group").Data(params).Save()
		if err == nil {
			newid, _ := re.LastInsertId()
			res["code"] = 0
			res["msg"] = "保存成功"
			res["data"] = newid
		} else {
			res["code"] = -1
			res["msg"] = err.Error()
		}
	} else {
		res["code"] = -1
		res["msg"] = "您没有该操作权限"
	}
	r.Response.WriteJson(res)
	r.ExitAll()
}

func (a *group) DELETE(r *ghttp.Request) {
	res := g.Map{
		"code":  0,
		"msg":   "",
		"data":  nil,
		"count": 0,
	}
	params := r.GetFormMap()

	id, exist := params["id"]
	if exist {
		claims := jwt.ExtractClaims(r)
		current_user := claims["userid"].(string)
		url := r.URL.Path
		params["lpt"] = gtime.Now().String()
		params["whom"] = current_user
		//db:=g.DB(g.Cfg().GetString("custom.db_name"))
		if dao.CheckApiPermission(url, r.Method, current_user) {
			//有场区的团区无法直接删除
			areas, _ := dao.T_area.GetList(fmt.Sprintf("group_id=%s", gconv.String(id)))
			if len(areas) > 0 {
				res["code"] = -1
				res["msg"] = "存在下级区域，无法删除"
			} else {
				_, err := dao.T_group.Delete(gconv.String(id))
				if err != nil {
					g.Log().Line().Error(err)
					res["code"] = -1
					res["msg"] = err.Error()
				}
			}
		} else {
			res["code"] = -1
			res["msg"] = "您没有该操作权限"
		}
	} else {
		res["code"] = -1
		res["msg"] = "错误的参数"
	}

	r.Response.WriteJson(res)
	r.ExitAll()
}
